<template>
    <div>
        <el-row :gutter="30">
            <el-col :span="16">
                <el-card shadow="hover" style="height:500px;width:1000px;">
                    <schart style="padding-top:10px" ref="bar" class="schart" canvasId="bar" :options="options" :key="updateKey"></schart>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import Schart from "vue-schart";
import { ref, reactive } from "vue";
import { getBaseData, getBarData } from '../../api'
export default {
    name: "statistic-bar",
    components: { Schart },
    setup() {
        const baseData = ref({});
        const updateKey = ref(0);
        const options = {
            type: "bar",
            title: {
                text: "最近7天药品销售图",
            },
            xRorate: 25,
            labels: ['2021/01/12', '2021/01/13', '2021/01/14', '2021/01/15', '2021/01/16', '2021/01/17', '2021/01/18'],
            datasets: [
                {
                    label: "销售金额",
                    data: [0, 1, 2, 3, 4, 5 ,6]
                },
                {
                    label: "药品销量",
                    data: [0, 1, 2, 3, 4, 5, 6]
                }
            ],
        };

        const fetchBaseData = () => {
            getBaseData().then(res => {
                if(res.code === 200) {
                    baseData.value = res.data;
                }
            });
            getBarData().then(res => {
                if (res.code === 200) {
                    let d = res.data.message;
                    options.labels = d.map(item => {return item.date;});
                    options.datasets[0].data = d.map(item => {return item.price;});
                    options.datasets[1].data = d.map(item => {return item.quantity;});
                    updateKey.value += 1;
                    console.log(options);
                }
            })
        };
        fetchBaseData();

        return {
            baseData,
            options,
            updateKey
        };
    },
};
</script>

<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 20px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 24px;
    color: #999;
    line-height: 40px;
}

.user-info-list span {
    margin-left: 70px;
}

.member span {
    display: inline-block;
    padding-top: 8px;
    padding-bottom: 8px;
}
.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 800px;
    height: 400px;
}
</style>
